﻿using System;
using System.Threading;
namespace SS
{
    partial class Form1
    {

        //结束或 开始 改变状态
        /// <ummary>
        /// 结束或 开始 改变状态
        /// </summary>
        /// <param name="b"></param>
        private void ChangeState(bool b)
        {
            if (this.txtConnect.InvokeRequired)
            {
                ChangeState cs = new ChangeState(ChangeState);
                this.Invoke(cs, b);
            }
            else
            {
                this.txtConnect.Enabled = b;
            }

            if (this.txtThreadNum.InvokeRequired)
            {
                ChangeState cs = new ChangeState(ChangeState);
                this.Invoke(cs, b);
            }
            else
            {
                this.txtThreadNum.Enabled = b;
            }

            if (this.txtTime.InvokeRequired)
            {
                ChangeState cs = new ChangeState(ChangeState);
                this.Invoke(cs, b);
            }
            else
            {
                this.txtTime.Enabled = b;
            }
            if (this.btnStart.InvokeRequired)
            {
                ChangeState cs = new ChangeState(ChangeState);
                this.Invoke(cs, b);
            }
            else
            {
                this.btnStart.Enabled = b;
            }
            if (this.cboProxy.InvokeRequired)
            {
                ChangeState cs = new ChangeState(ChangeState);
                this.Invoke(cs, b);
            }
            else
            {
                this.cboProxy.Enabled = b;
            }
            if (this.btnStop.InvokeRequired)
            {
                ChangeState cs = new ChangeState(ChangeState);
                this.Invoke(cs, true);
            }
            else
            {
                this.btnStop.Enabled = true;
            }
            
            
        }

        //改变 投票ID 的文本，文本框显示和隐藏
        private void SetVoteIdVisible(bool b)
        {
            if (this.lblVoteId.InvokeRequired)
            {
                ChangeState cs = new ChangeState(ChangeState);
                this.Invoke(cs, b);
            }
            else
            {
                this.lblVoteId.Visible = b;
            }

            if (this.txtAid.InvokeRequired)
            {
                ChangeState cs = new ChangeState(ChangeState);
                this.Invoke(cs, b);
            }
            else
            {
                this.txtAid.Visible = b;
            }
        }









        //总数
        /// <summary>
        /// 总数
        /// </summary>
        /// <param name="text"></param>
        private void SetSumNum(string text)
        {
            if (this.txtSumNum.InvokeRequired)
            {
                SetTextCallback set = new SetTextCallback(SetSumNum);
                this.Invoke(set, text);
            }
            else
            {
                this.txtSumNum.Text = text;
            }
        }
        //日志
        /// <summary>
        /// //日志
        /// </summary>
        private void Log()
        {
            if (this.rtbResult.InvokeRequired)
            {
                Callback set = new Callback(Log);
                this.Invoke(set);
            }
            else
            {

                VoteLog.AddResult("result.fei", this.rtbResult.Text);
                this.rtbResult.Clear();
            }
        }

        //给值
        /// <summary>
        ///  给值
        /// </summary>
        /// <param name="text"></param>
        private void SetText(string text)
        {
            if (this.rtbResult.InvokeRequired)
            {
                SetTextCallback set = new SetTextCallback(SetText);
                this.Invoke(set, text);
            }
            else
            {
                this.rtbResult.AppendText(string.Format("{0}.{1} {2}{3}", voteCount.NumberCount++, DateTime.Now, text, "--X-->"+Thread.CurrentThread.Name + Environment.NewLine));
                this.rtbResult.ScrollToCaret();
                if (voteCount.NumberCount % 2000 == 0)
                {
                    GetNum();

                }
                if (voteCount.NumberCount % 4000 == 0)
                {
                    Log();
                }
            }
        }
        //宽带连接
        /// <summary>
        /// 宽带连接
        /// </summary>
        private void Connect()
        {
            if (isStop)
            {
                return;
            }
            try
            {
                RASDisplay ras = new RASDisplay();
                ras.Disconnect();//断开连接
                this.SetText("正在执行连接中ing...");
                int connect = ras.Connect(strConnect);//重新拨号
                Uri uriString = new Uri("http://www.baidu.com/");
                byte[] responseData = webClientQu.DownloadData(uriString);
            }
            catch (Exception e)
            {
                this.SetText("error:宽带连接错误" + e.Message);
            }

        }
 
    }
}
